<?php
namespace app\admin\controller;

use app\common\model\User as UserModel;
use app\common\controller\AdminBase;
use think\Config;
use think\Db;

/**
 * 用户管理
 * Class AdminUser
 * @package app\admin\controller
 */
class User extends AdminBase
{
    protected $user_model;

    protected function initialize()
    {
        parent::initialize();
        $this->user_model = new UserModel();
    }

    /**
     * 用户管理
     * @param string $keyword
     * @param int    $page
     * @return mixed
     */
    public function index($tg_name='',$keyword = '', $page = 1)
    {
        $map = [];
        if ($keyword) {
            $map[] = ['username','like', "%{$keyword}%"];
        }
		if($tg_name)
		{
			$map[] = ['tg_name','like', "%{$tg_name}%"];
		}
        $user_list = $this->user_model->where($map)->order('id DESC')->paginate(15, false, ['page' => $page]);

        return $this->fetch('index', ['user_list' => $user_list, 'keyword' => $keyword,'tg_name'=>$tg_name]);
    }

    /**
     * 添加用户
     * @return mixed
     */
    public function add()
    {
        return $this->fetch();
    }

    /**
     * 保存用户
     */
    public function save()
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();
            $validate_result = $this->validate($data, 'User');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $data['password'] = password_hash($data['pass'],PASSWORD_DEFAULT);
                if ($this->user_model->allowField(true)->save($data)) {
                    $this->success('保存成功');
                } else {
                    $this->error('保存失败');
                }
            }
        }
    }

    /**
     * 编辑用户
     * @param $id
     * @return mixed
     */
    public function edit($id)
    {
        $user = $this->user_model->find($id);
        return $this->fetch('edit', ['user' => $user]);
    }

    /**
     * 更新用户
     * @param $id
     */
    public function update($id='')
    {
        if ($this->request->isPost()) {
            $data            = $this->request->post();

            $validate_result = $this->validate($data, 'User');

            if ($validate_result !== true) {
                $this->error($validate_result);
            } else {
                $user           = $this->user_model->find($id);
                $user->id       = $id;
                $user->status   = $data['status'];
                $user->platform = $data['platform'];
                $user->username = $data['username'];
                $user->nickname = $data['nickname'];
                $user->email    = $data['email'];
                $user->mobile   = $data['mobile'];
                if (!empty($data['password'])) {
                    $user->password = password_hash($data['password'],PASSWORD_DEFAULT);
                }
                if ($user->save() !== false) {
                    $this->success('更新成功');
                } else {
                    $this->error('更新失败');
                }
            }
        }
    }

    /**
     * 删除用户
     * @param $id
     */
    public function delete($id)
    {
        if ($this->user_model->destroy($id)) {
            $this->success('删除成功');
        } else {
            $this->error('删除失败');
        }
    }
	
	/**
	 * 查询用户是否存在，并返回推广员信息
	 */
	public function checkUser()
	{
		$username=input('username');
		$user=$this->user_model->where('username',$username)->find();
		if(empty($user))return json(['code'=>'0','msg'=>'账号不存在']);
		return json(['code'=>'1','tg_name'=>$user['tg_name']]);
	}
}